package handlers

import (
	"github.com/gin-gonic/gin"
	"order-master1/models"
	"strconv"
)

func AddRoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	if rid == 0 || nid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	role_nodes := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}

	err := models.DB.Create(&role_nodes).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func FindRoleNode(c *gin.Context) {
	id := c.Query("id")

	var node models.Node
	models.DB.Table("nodes").
		Select("*").
		Joins("LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("role_nodes.rid=?", id).
		Find(&node)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": node,
	})
	return
}
